/**
 * Created by wyy on 17-7-10.
 */

angular.module('tribalc').controller(
    'CompanyBaseController',
    ['$rootScope', '$scope', '$state', '$stateParams', 'Api', '$location', 'Alert', function ($rootScope, $scope, $state, $stateParams, Api, $location, Alert) {

        window.onload= setupBaseMinHeight();
        checkLoginWhenChangePage();

        gotoDefaultPage();

        $rootScope.showAlert = false;

        $(document).resize(function(){
            $scope.heights= {
                "height" : $('.base-content-main').height()+'px'
            };
        });

        function setupBaseMinHeight() {

            $scope.heights= {
                "height" : $('.base-content-main').height()+'px'
            };
        }


        function gotoDefaultPage() {
            if (!Api.getAssign()) {
                return false;
            }
            if ($state.current.name == 'company') {
                $state.go('company.companyApartmentManage');
            }
        }


        function checkLoginWhenChangePage() {

            checkLogin();


            // $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
            //
            //
            //         checkLogin();
            //
            // });
        }

        function checkLogin() {

            var urls = ['/login', '/register', '/password'];
            if (urls.indexOf($location.path()) != -1) {
                return
            }
            if (!Api.getAssign()) {
                Alert.alertBox('您还未登录');
                localStorage.clear();
                $state.go('login');
                // throw new Error("Login out!");
            }
        }

        // getApartmentList();
        // function getApartmentList() {
        //     Api.getCompanyDetail(null,function (result) {
        //         Api.getApartmentList(result.data.communityId,function (result) {
        //             console.log(result);
        //         })
        //     });
        // }
        //  getAvailabilityList();
        // function getAvailabilityList() {
        //     Api.getCompanyDetail(null,function (result) {
        //         // console.log(result.data.communityId);
        //         Api.getApartmentList(result.data.communityId,function (apartsResult) {
        //             // console.log(apartsResult.data[0].id);
        //             Api.getAvailabilityList(result.data.communityId,apartsResult.data[0].id,function (apartmentInfo) {
        //                 console.log(apartmentInfo);
        //             })
        //
        //         });
        //     });
        // }
        // var apartmentId=$stateParams.apartmentId
        // deleteApartmentInfo();
        // function deleteApartmentInfo() {
        //
        //         Api.deleteApartmentInfo(apartmentId,function (result) {
        //             console.log(result);
        //         })
        //
        //
        // }


    }]
);
